/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "sys_init.h"
#include "com_obj.h"
#include "common.h"
#include "bsp_hal.h"
#include "flash_cfg.h"
#include "flash_task.h"
#include "gd25q_spi.h"
#include "gd25q_cmd.h"

struct
{
    uint8_t *databuff;
    uint32_t sectr;
    uint32_t addr;
    uint8_t filldata;
    dword_tt dwbuff[1024];
    uint8_t nand_id[4];
    uint8_t read_all;
    uint16_t check_en;
    uint32_t dat;
    uint32_t err;
    uint8_t module;
} strobj;

/*****************************************************************************
*
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void  flash_check(ULONG thread_input)
{
    int index;
    
        if (1 == strobj.check_en)
        {
            uint32_t dats = strobj.dat = 5;
            
            flash_dbg_print("fix spi mode check : start \n");
            
            //全盘写入数据
            for (strobj.addr = 0; strobj.addr < GD25Q_CHIP_SIZE; strobj.addr += GD25Q_PAGE_SIZE)
            {
                if (strobj.addr % GD25Q_SECTOR_SIZE == 0)
                {
                    gd25_sector_erase(strobj.addr);
                }

                for ( index = 0; index < GD25Q_PAGE_SIZE / 4; index++)
                {
                    strobj.dwbuff[index].uVal = strobj.dat++;
                }

                gd25_write(strobj.addr, GD25Q_PAGE_SIZE, strobj.dwbuff[0].v);
                
                if(0 == (strobj.addr & 0x03FFFF))
                {
                    flash_dbg_print("fix spi mode check write data: %d %% \n", strobj.addr * 100 / GD25Q_CHIP_SIZE );
                }
            }

            strobj.dat = dats;
            flash_dbg_print("fix spi mode check : read and check \n");
            
            //全盘读回验证
            for (strobj.addr = 0; strobj.addr < GD25Q_CHIP_SIZE; strobj.addr += GD25Q_PAGE_SIZE)
            {
                if (strobj.addr % GD25Q_SECTOR_SIZE == 0)
                {
                    gd25_read(strobj.addr, GD25Q_SECTOR_SIZE, strobj.dwbuff[0].v);

                    for ( index = 0; index < GD25Q_SECTOR_SIZE / 4; index++)
                    {
                        if (strobj.dwbuff[index].uVal != strobj.dat)
                        {
                            strobj.err++;
                        }
                        strobj.dat++;
                    }
                    
                    if(strobj.err)
                    {
                        flash_dbg_print("fix spi mode check err: %d @ %x \n", strobj.err, strobj.addr);
                        strobj.err = 0;
                    }
                }
            }

            strobj.check_en = 0;
            flash_dbg_print("fix spi mode check : end \n");
        }
}
